package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.BookDao;
import com.itzz.spring_zhoneHeng.dao.ChaptersDao;
import com.itzz.spring_zhoneHeng.pojo.Book;
import com.itzz.spring_zhoneHeng.pojo.Chapters;
import com.itzz.spring_zhoneHeng.service.ChaptersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/chapters")
public class ChaptersController {

    @Autowired
    ChaptersDao dao;
    @Autowired
    ChaptersService service;



    //根据书名查询章节
    @RequestMapping("/queryAll")
    public RestResponse queryAll(Chapters chapters, Integer pageNum, Integer pageSize) {
        RestResponse response = service.queryAll(chapters, pageNum, pageSize);
        return response;
    }

    //回显书名和章节
    @RequestMapping("/queryChaptersById/{id}")
    public RestResponse queryChaptersByName(@PathVariable Integer id) {
        RestResponse response = service.queryChaptersById(id);
        return response;
    }

    //修改书名和章节名及其状态
    @RequestMapping("/update")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse update(Chapters chapters) {
        RestResponse response = service.update(chapters);
        return response;
    }

    //查询所有的书籍
    @RequestMapping("/queryAllBook")
    public RestResponse queryAllBook() {
        List<Book> books = dao.queryAll();
        RestResponse response = RestResponse.ok(books);
        return response;
    }

    //添加章节及其审核状态
    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse add(Chapters chapters,Integer bookId) {
        RestResponse response = service.add(chapters,bookId);
        return response;
    }

    //根据id删除
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse delete(@PathVariable("id") Integer id) {
        int i = dao.deleteById(id);
        if (i == 0) {
            return RestResponse.fail(400, "删除失败");
        }
        return RestResponse.ok(i);
    }

}
